/*
 * ServerMain.cpp
 *
 *  Created on: 2014-1-7
 *      Author: root
 */
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include<stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <fstream>

#include "networkServer/NetWorkServer.h"
#include "gameLogicServer/GameLogicServer.h"
#include "database/MySqlManager.h"
#include <mysql/mysql.h>

//#define COUT_RD 1

using namespace std;
using namespace miniGameServer;

int main(int argc,char **argvs)
{
	/*union test{
		char a;
		char b;
		int c;
		char e[4];
	};
	union test tt;
	tt.a = 'a';
	*(&tt.b + 1) = 'b';
	cout<<tt.c<<endl;
	cout<<tt.e[1]<<endl;
	return 1;*/

	if(argc <3)
	{
		cout<<"need 2 argvs,host,port"<<endl;
		exit(1);
	}

	signal(SIGPIPE,SIG_IGN);

	int port = atoi(argvs[2]);
	char *host = argvs[1];

#ifdef COUT_RD
	ofstream Log("log.txt");
	streambuf * out = cout.rdbuf(Log.rdbuf());
#endif

	cout<<"argc "<<argc<<endl;
	cout<<"beging server"<<endl;
	miniGameServer::NetWorkServer networkS;
	networkS.startServer(host,port);
	miniGameServer::GameLogicServer logicS;
	logicS.startServer();

	int result = miniGameServer::MySqlManager::instance().connect("zhao","123456","test");
	if(result == 0)
	{
		MySqlManager::instance().selectQuery("select * from users");
		char ** row = MySqlManager::instance().fetchRow();
		for(int i = 0;i<3;i++)
		{
			cout<<row[i]<<'\t';
		}
		cout<<endl;
	}


	while(true)
	{
		if(getchar() == 'q')
		{
			break;
		}
	}

	/*
	 * logic关闭到比较快
	 */
	logicS.stopServer();

	cout<<"main thread is stoping,wait for others "<<endl;
	networkS.stopServer();
#ifdef COUT_RD
	cout.rdbuf(out);
	Log.close();
#endif

	cout<<"!!!!!!!!!!!!!!!!!!!!!!!all server stoped!!!!!!!!!!!!!!!!!!!!!!"<<endl;

	return 0;
}
